<?php
namespace App\Services\Wechat;
class Yunpay
{

    public function __construct()
    {
        $this->yun_config = [
            'partner' => '6525146226211270',            //合作身份者id
            'key' => 'Dv8KQBrDTVhFLmAAAMHvD4fSSQfGGE7X' //安全检验码
        ];

        $this->seller_email = '197508705@qq.com';   //云会员账户（邮箱）
    }


    public function yunpay($openid, $sn, $money, $type = 1)
    {
        if($money < 1)
        {
            $money = 1;
        }
        if($type == 1)
        {
            //订单名称
            $subject = '一元捡宝-商品支付';//必填
            //订单描述
            $body = '一元捡宝商品支付';
        }
        else
        {
            //订单名称
            $subject = '一元捡宝-充值';//必填
            //订单描述
            $body = '一元捡宝充值';
        }
        //商户订单号
        $out_trade_no = $sn;//商户网站订单系统中唯一订单号，必填
        //付款金额
        $total_fee = $money;//必填 需为整数

        //服务器异步通知页面路径
        $datas = $type.'-----'.$openid;
        $datas = encode($datas);
        //$datas = base64_encode($datas);
        $nourl = 'http://yiyuanshibao.com/w/pay/no/'.$datas;
        //需http://格式的完整路径，不能加?id=123这类自定义参数

        //页面跳转同步通知页面路径
        $reurl = 'http://yiyuanshibao.com/w/pay/re/'.$datas;
        //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/

        //商品展示地址
        $orurl = "";
        //需http://格式的完整路径，不能加?id=123这类自定义参数，如原网站带有 参数请彩用伪静态或短网址解决

        //商品形象图片地址
        $orimg = "";
        //需http://格式的完整路径，必须为图片完整地址

        /************************************************************/

        //构造要请求的参数数组，无需改动
        $parameter = array(
            "partner" => trim($this->yun_config['partner']),
            "seller_email"	=> $this->seller_email,
            "out_trade_no"	=> $out_trade_no,
            "subject"	=> $subject,
            "total_fee"	=> $total_fee,
            "body"	=> $body,
            "nourl"	=> $nourl,
            "reurl"	=> $reurl,
            "orurl"	=> $orurl,
            "orimg"	=> $orimg
        );

        //建立请求
        $html_text = $this->i2e($parameter, "支付进行中...");
        return $html_text;
    }


    /**
     *
     *yun_md5.function.php
     */
    public function md5Verify($i1, $i2, $i3)
    {
        $prestr = $i1 . $i2 . $this->yun_config['partner'] . $this->yun_config['key'];
        $mysgin = md5($prestr);

        if ($mysgin == $i3) {
            return true;
        } else {
            return false;
        }
    }


    public function i2e($parameter, $subm)
    {
        $myparameter = '';
        foreach ($parameter as $pars) {
            $myparameter .= $pars;
        }
        $sign = md5($myparameter . 'i2eapi' . $this->yun_config['key']);
        $mycodess = "<form name='yunsubmit' action='http://pay.yunpay.net.cn/i2eorder/yunpay/' accept-charset='utf-8' method='get'><input type='hidden' name='body' value='" . $parameter['body'] . "'/><input type='hidden' name='out_trade_no' value='" . $parameter['out_trade_no'] . "'/><input type='hidden' name='partner' value='" . $parameter['partner'] . "'/><input type='hidden' name='seller_email' value='" . $parameter['seller_email'] . "'/><input type='hidden' name='subject' value='" . $parameter['subject'] . "'/><input type='hidden' name='total_fee' value='" . $parameter['total_fee'] . "'/><input type='hidden' name='nourl' value='" . $parameter['nourl'] . "'/><input type='hidden' name='reurl' value='" . $parameter['reurl'] . "'/><input type='hidden' name='orurl' value='" . $parameter['orurl'] . "'/><input type='hidden' name='orimg' value='" . $parameter['orimg'] . "'/><input type='hidden' name='sign' value='" . $sign . "'/></form><script>document.forms['yunsubmit'].submit();</script>";
        return $mycodess;
    }

    public function hifun($string, $operation, $key = '')
    {
        $key = md5('hifun2013');
        $key_length = strlen($key);
        $string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
        $string_length = strlen($string);
        $rndkey = $box = array();
        $result = '';
        for ($i = 0; $i <= 255; $i++) {
            $rndkey[$i] = ord($key[$i % $key_length]);
            $box[$i] = $i;
        }
        for ($j = $i = 0; $i < 256; $i++) {
            $j = ($j + $box[$i] + $rndkey[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        for ($a = $j = $i = 0; $i < $string_length; $i++) {
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
        }
        if ($operation == 'D') {
            if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8))
                return substr($result, 8);
            else
                return '';
        } else {
            return str_replace('=', '', base64_encode($result));
        }
    }

}